/**
 * base js
 */
var _baseEvent = function () {
    "use strict";
    this._baseEventPool = {};

    this.setBaseEvent = function (eventName, event) {
        this._baseEventPool[eventName] = event;
        return this;
    }

    this.runBaseEvent = function (eventName) {
        if (this.hasBaseEvent(eventName)) {
            var eventFun = this._baseEventPool[eventName];
            var _arguments = '';
            var _argumentIndex = [];
            var _argumentValue = [];
            if (typeof arguments[1] != 'undefined' && arguments[1].constructor == Array) {
                for (var argumentId in arguments[1]) {
                    _argumentValue[argumentId] = arguments[1][argumentId];
                    _argumentIndex[argumentId] = '_argumentValue[' + argumentId + ']';
                }
                var _arguments = _argumentIndex.join(',');
            }
            var runCommand = 'eventFun(' + _arguments + ')';
            return eval(runCommand);
        }
        return undefined;
    }

    this.hasBaseEvent = function (eventName) {
        return this._baseEventPool.hasOwnProperty(eventName);
    }

}

var _baseComponent = function () {
    "use strict";
    _baseEvent.apply(this);
    _baseHash.apply(this);
    this.component = function (name, component) {
        this[name] = new component;
    }
}

var _baseHash = function () {
    this._baseHashData = {}

    this.setHash = function (key, value) {
        return this._baseHashData[key] = value;
    }

    this.getHash = function (key) {
        return this._baseHashData[key];
    }

    this.hasHash = function (key) {
        return this._baseHashData.hasOwnProperty(key);
    }

    this.delHash = function (key) {
        delete this._baseHashData[key];
    }

    this.clearHash = function () {
        return this._baseHashData = {}
    }

    this.getHashAll = function () {
        return this._baseHashData;
    }

}

/**
 * client js
 */
var _client = function () {
    _baseComponent.apply(this);
}

client = (function (obj) {
    "use strict";
    var _obj = false;
    if (_obj === false) {
        _obj = new obj();
    }
    return _obj;
})(_client);

//lastTime
(function (client) {

    var baseEngine = function () {
        _baseComponent.apply(this);
        this.cycle = 1;
        this.run = function () {

        }
    }

    var baseAjaxEngine = function () {
        baseEngine.apply(this);
        var _this = this;
        this.successList = {}
        this.requestData = {}
        this.requestConfig = {
            'type': 'post',
            'dataType': 'json',
            'success': false
        }


        this.getRequestConfig = function () {
            if (typeof this.requestData == 'function') {
                this.requestConfig['data'] = this.requestData();
            } else {
                this.requestConfig['data'] = this.requestData;
            }


            return this.requestConfig;
        }

        this._construct = function () {
            _this.setBaseEvent('ajax.request.before', function () {
                return true;
            });
        }

        this._requestSuccess = function (response) {
            for (var successEvent in _this.successList) {
                _this.runBaseEvent(successEvent, [response]);
            }
        }

        this.setResponseEvent = function (eventName, event) {
            var responseEventName = 'ajax.' + eventName;
            _this.setBaseEvent(responseEventName, event);
            _this.successList[responseEventName] = responseEventName;
            return _this;
        }

        this.setRequestData = function (requestData) {
            this.requestData = requestData;
            return _this;
        }

        this.addRequestData = function (requestName, requestData) {
            this.requestData[requestName] = requestData;
            return _this;
        }

        this.setRequest = function (request) {

            for (var requestName in request) {
                if (requestName == 'success') {
                    _this.setBaseEvent('ajax.suceess', request['success']);
                    _this.successList['ajax.suceess'] = 'ajax.suceess';
                    this.requestConfig['success'] = _this._requestSuccess;
                    continue;
                }

                if (requestName == 'data') {
                    this.setRequestData(request['data']);
                    continue;
                }

                this.requestConfig[requestName] = request[requestName];
            }
            return this;
        }

        this.run = function () {
            if (_this.runBaseEvent('ajax.request.before', [_this])) {
                $.ajax(_this.getRequestConfig());
            }

        }

        this._construct();

    }
    client.component('engine', function () {
        "use strict";
        _baseComponent.apply(this);
        var _this = this;

        this._timerList = {}
        this._timerRunPool = {}

        this._rely = {
            'base': 'baseEngine',
            'ajax': 'baseAjaxEngine',
        }

        this.createTimer = function (timerName, cycle, timerType) {
            var timerClass = null;
            if (typeof timerType == 'undefined') {
                timerType = 'base';
            }

            if (typeof timerType == 'string') {

                if (_this._rely.hasOwnProperty(timerType)) {
                    timerClass = _this._rely[timerType];
                } else {
                    timerClass = timerType;
                }
            } else {
                timerClass = timerType;
            }

            var timerObj = eval('new ' + timerClass + '()');
            timerObj.cycle = cycle;
            _this.addTimer(timerName, timerObj);
            return timerObj;
        }

        this.createTimerNG = function (timerName, cycle, timerType) {
            var timerObj = false;
            if (this.hasTimer(timerName)) {
                timerObj = this.selectTimer(timerName);
            } else {
                timerObj = this.createTimer(timerName, cycle, timerType);
            }
            return timerObj;
        }

        this.addTimer = function (timerName, engine) {
            _this._timerList[timerName] = engine;
        }

        this.selectTimer = function (timerName) {
            return _this._timerList[timerName];
        }

        this.hasTimer = function (timerName) {
            this._timerList.hasOwnProperty(timerName);
        }

        this.config = function (key, value) {
            if (typeof value == 'undefined') {
                return _this.getHash(key);
            } else {
                _this.setHash(key, value);
                return this;
            }
        }

        this.run = function (timerList) {
            var timerPool = {};
            if (typeof timerList == 'undefined') {
                timerPool = _this._timerList;
            } else {
                for (var timerId in timerList) {
                    var tempTimerName = timerList[timerId];
                    timerPool[tempTimerName] = tempTimerName;
                }
            }

            for (var timerName in timerPool) {
                clearInterval(_this._timerRunPool[timerName]);
                _this._timerRunPool[timerName] = setInterval(_this.selectTimer(timerName).run, _this.selectTimer(timerName).cycle * 1000);
            }
        }

        this.runOnce = function (timerList) {
            var timerPool = {};
            if (typeof timerList == 'undefined') {
                timerPool = _this._timerList;
            } else {
                for (var timerId in timerList) {
                    var tempTimerName = timerList[timerId];
                    timerPool[tempTimerName] = tempTimerName;
                }
            }

            for (var timerName in timerPool) {
                _this.selectTimer(timerName).run()
            }
        }

    });

})(client);

//auction namespace
(function (client, $) {

    var auctionDom = function () {
        "use strict";
        _baseComponent.apply(this);
        var _this = this;
        this._rootDom = false;
        this._currentAuction;
        this._template = null;
        this._construct = function () {

        }

        this.setTemplate = function (templateHtml) {
            this._template = templateHtml;
            return this;
        }

        this.buildHtmlTemplate = function (auctionData) {
            var auctionHtml = $(this._template);
            this.setCurrentAuction(auctionHtml);
            for (var auctionDataAttribute in auctionData) {
                this.setAttribute(auctionDataAttribute, auctionData[auctionDataAttribute]);
            }
            return auctionHtml;
        }

        /*
         this.auctionHtmlByTemplate = function(auctionData){
         var selectAuction = _this.auction(auctionData.id);
         for(var auctionDataAttribute in auctionData){
         selectAuction.setAttribute(auctionDataAttribute,auctionData[auctionDataAttribute]);
         }
         }
         */

        this.auctionDomIds = function () {
            var auctionDomIds = []
            this.root().find('[auction-id]').each(function () {
                var auctionId = $(this).attr('auction-id');
                auctionDomIds.push(auctionId);
            });
            return auctionDomIds;
        }

        this.root = function (rootDom) {
            if (typeof rootDom == 'undefined') {
                if (_this._rootDom === false) {
                    _this._rootDom = 'body';
                }
                var rootDomObj = $(_this._rootDom);
                return rootDomObj;
            } else {
                this._rootDom = rootDom;
                return this;
            }
        }

        this.auction = function (id) {
            this.setCurrentAuction(this.root().find('[auction-id="' + id + '"]'));
            return this;
        }

        this.setCurrentAuction = function (dom) {
            this._currentAuction = dom;
            return this;
        }

        this.getCurrentAuction = function () {
            return this._currentAuction;
        }

        this.setAttribute = function (attribute, value) {
        	
        	var auctionDom = this.getCurrentAuction();
            var attributeDom = auctionDom.find('[attribute="' + attribute + '"]');
        	
        	if (_this.hasBaseEvent(attribute + '.dom.setAttributeBefore')) {
                _this.runBaseEvent(attribute + '.dom.setAttributeBefore', [value, attribute, attributeDom, auctionDom]);
            }
            
            if (attribute == 'id') {
                auctionDom.attr('auction-id', value);
            }
            if (attributeDom.length <= 0) {
                auctionDom.attr('attribute-data-' + attribute, value);
            } else {
                var run = null;
                switch (true) {
                    case attributeDom.is('img'):
                        run = attributeDom.attr('src', value);
                        break;
                    case attributeDom.is('a'):
                        run = attributeDom.attr('href', value);
                        break
                    case attributeDom.is('input'):
                        run = attributeDom.val(value);
                        break;
                    default:
                        var oldValue = attributeDom.html();

                        if (_this.hasBaseEvent(attribute + '.dom.setFormat')) {
                            value = _this.runBaseEvent(attribute + '.dom.setFormat', [value, attribute]);
                        }

                        run = attributeDom.html(value);

                        if (oldValue != value) {
                            var attributeAnimation = auctionDom.attr('attribute-animation');
                            if (typeof attributeAnimation != 'undefined') {
                                var attributeAnimationValue = attributeAnimation.split(':');
                                var attributeAnimationName = attributeAnimationValue[0];
                                var attributeAnimationTime = attributeAnimationValue[1];
                                attributeDom.addClass(attributeAnimationName);
                                var animation = setTimeout(function () {
                                    auctionAttribute.removeClass(attributeAnimation);
                                }, attributeAnimationTime * 1000);
                            }
                        }

                        break;
                }
            }
            if (_this.hasBaseEvent(attribute + '.dom.setAttributeAfter')) {
                _this.runBaseEvent(attribute + '.dom.setAttributeAfter', [value, attribute, attributeDom, auctionDom]);
            }
            return this;
        }

        this.getAttribute = function (attribute) {
            var auctionDom = this.getCurrentAuction();
            var attributeDom = auctionDom.find('[attribute="' + attribute + '"]');
            var attributeValue = null;
            if (attributeDom.length <= 0) {
                attributeValue = auctionDom.attr('attribute-data-' + attribute);
            } else {
                switch (true) {
                    case attributeDom.is('img'):
                        attributeValue = attributeDom.attr('src');
                        break;
                    case attributeDom.is('a'):
                        attributeValue = attributeDom.attr('href');
                        break
                    case attributeDom.is('input'):
                        attributeValue = attributeDom.val();
                        break;
                    default:
                        attributeValue = attributeDom.html();
                        break;
                }
            }
            if (_this.hasBaseEvent(attribute + '.dom.getFormat')) {
                attributeValue = _this.runBaseEvent(attribute + '.dom.getFormat', [attributeValue, attribute]);
            }
            return attributeValue;
        }

        this._construct();

    }

    var dispatcher = function () {
        "use strict";
        _baseComponent.apply(this);
        var _this = this;

        this._construct = function (auctionId) {

        }

        this.dispatcher = function (attribute, value, auctionDataObj) {
            var dispatcherEvent = _this.hasBaseEvent(attribute + '.Dispatcher') ? attribute + '.Dispatcher' : '__init.Dispatcher';
            _this.runBaseEvent(dispatcherEvent, [attribute, value, auctionDataObj]);
        }


        this._construct();
    }

    var shelf = function () {
        "use strict";
        _baseComponent.apply(this);
        var _this = this;
        this.name = '';
        this._dispatcher = false;
        this._dom = false;
        this.limit = 6;
        this._baseRules = {
            'data': function (auction) {
                return client.auction.has(auction.id);
            },
            'unique': function (auction) {
                return !_this.hasAuctionId(auction.id);
            },
            'limit': function (auction) {
                if (_this.limit == -1) {
                    return true;
                }
                var itemNaumber = 1;
                return itemNaumber < _this.limit;
            },
            'custom': function (auction) {
                var valid = true;
                if (_this.hasBaseEvent('valid.putOn')) {
                    valid = _this.runBaseEvent('valid.putOn', [auction]);
                }
                return valid;
            },
            'end_time': function (auction) {
                var maxAuctionEndTime = 0;
                for (var auctionId in _this._auctionPool) {
                    var auctionEndTime = client.auction.get(auctionId).getAttribute('end_time');
                    if (maxAuctionEndTime < auctionEndTime) {
                        maxAuctionEndTime = auctionEndTime;
                    }
                }
                return auction.getAttribute('end_time') >= maxAuctionEndTime;
            }
        }

        this._construct = function () {
            this.dispatcher().setBaseEvent('__init.Dispatcher', function (attribute, value, auctionDataObj) {
                var auctionId = auctionDataObj.id;
                _this.auctionDom().auction(auctionId).setAttribute(attribute, value);
            });
        }

        this.auctionDom = function () {
            if (this._dom === false) {
                this._dom = new auctionDom();
            }
            return this._dom;
        }

        this.setAuctionId = function (auctionId) {
            _this.setHash(auctionId, auctionId);
        }

        this.delAuctionId = function (auctionId) {
            _this.delHash(auctionId);
        }

        this.hasAuctionId = function (auctionId) {
            _this.hasHash(auctionId);
        }

        this.allAuctionId = function () {
            return _this.getHashAll();
        }

        this.clearAuctionId = function () {
            return _this.clearHash();
        }

        this.allAuctionIdCount = function () {
            var allAuctionId = _this.allAuctionId();
            var number = 0;
            for (var auctionId in allAuctionId) {
                number += 1;
            }
            return number;
        }

        this.dispatcher = function () {
            if (this._dispatcher === false) {
                this._dispatcher = new dispatcher();
            }
            return this._dispatcher;
        }

        this.putOn = function (auction) {
            var putOnValid = true;
            for (var rule in this._baseRules) {
                if (putOnValid) {
                    putOnValid = _this._baseRules[rule](auction);
                }
            }
            if (putOnValid) {
                this.setAuctionId(auction.id);
                auction.broadcasts().addDispatcherList(_this.name, _this.dispatcher().dispatcher);
                if (_this.hasBaseEvent('putOn.after')) {
                    attributeValue = _this.runBaseEvent('putOn.after', [auction]);
                }
            }

            return this;
        }

        this.putOff = function (auctionId) {
        	this.delAuctionId(auctionId);
        	this.auctionDom().auction(auctionId).setAttribute('last_date','End');
        	$("[auction-id="+auctionId+"]").find(".auction-sold-img").show();
        	if (_this.hasBaseEvent('putOff.after')) {
                attributeValue = _this.runBaseEvent('putOff.after', [auctionId]);
            }
        }

        this.maxEndTime = function () {
            var auctionIds = _this.allAuctionId();
            var end_time = 0;
            for (var auctionId in auctionIds) {
                var temp_end_time = client.auction.get(auctionId).getAttribute('end_time');
                if (end_time < temp_end_time) {
                    end_time = temp_end_time;
                }
            }
            return end_time;
        }

        this.shelfStatus = function () {
            var status = {}
            status['name'] = _this.name;
            status['limit'] = _this.limit;
            status['itemNumber'] = _this.allAuctionIdCount();
            status['needs'] = _this.limit - _this.allAuctionIdCount();
            status['ids'] = _this.allAuctionId();
            status['checkData'] = _this.checkData;
            status['maxEndTime'] = _this.maxEndTime();
            return status;
        }

        this.check = function () {

            if (_this.hasBaseEvent('check')) {
                return _this.hasBaseEvent('check'[this]);
            }

            var shelfStatus = _this.shelfStatus();
            var poolPage=client.getHash("poolPage");
            if(poolPage=="undefined"){
            	poolPage=1;
            }
            if (shelfStatus.needs > 0 && poolPage==1) {

                var noIDs = [];
                var ids = shelfStatus.ids;
                if(client.getHash('detailID')!==undefined){
                	noIDs.push(client.getHash('detailID'));
                }
                for (var id in ids) {
                    noIDs.push(id);
                }

                var sendShelfStatus = {
                    'name': shelfStatus.name,
                    'needs': shelfStatus.needs,
                    'noIds': noIDs.join('|'),
                    'checkData': shelfStatus.checkData,
                    'endTime': shelfStatus.maxEndTime
                }
                return sendShelfStatus;
            } else {
                return false;
            }

        }

        this.initDomData = function () {
            this.clearAuctionId();
            var auctionIds = this.auctionDom().auctionDomIds();
            for (var auctionIdIndex in auctionIds) {
                var auctionId = auctionIds[auctionIdIndex];
                var auctionObj = client.auction.createNG({'id': auctionId, 'status': 1});
                var auctionAttributes = auctionObj.getAttributes();
                var auctionInitData = {}
                var auctionDomSelect = _this.auctionDom().auction(auctionId);
                for (var attribute in auctionAttributes) {
                    auctionInitData[attribute] = auctionDomSelect.getAttribute(attribute);
                }
                auctionObj.setAttributes(auctionInitData);
                _this.putOn(auctionObj);

            }
            return _this;
        }

        this.init = function (init) {
            if (init.hasOwnProperty('rootSelector')) {
                _this.auctionDom().root(init['rootSelector']);
            }
            if (init.hasOwnProperty('template')) {
                _this.auctionDom().setTemplate(init['template']);
            }
            if (init.hasOwnProperty('checkData')) {
                this.checkData = init['checkData'];
            }
            if (init.hasOwnProperty('limit')) {
                this.limit = init['limit'];
            }
        }

        this._construct();
    }

    var detail = function () {
        "use strict";
        _baseComponent.apply(this);
        var _this = this;
        this.name = '';
        this._dispatcher = false;
        this._dom = false;

        this._construct = function () {
            this.dispatcher().setBaseEvent('__init.Dispatcher', function (attribute, value, auctionDataObj) {
                var auctionId = auctionDataObj.id;
                _this.auctionDom().auction(auctionId).setAttribute(attribute, value);
            });

        }

        this.auctionDom = function () {
            if (this._dom === false) {
                this._dom = new auctionDom();
            }
            return this._dom;
        }
        this.dispatcher = function () {
            if (this._dispatcher === false) {
                this._dispatcher = new dispatcher();
            }
            return this._dispatcher;
        }
        this.setAuctionDispatcher = function (auctionData) {
            auctionData.broadcasts().addDispatcherList(_this.name, _this.dispatcher().dispatcher);
        }
        this.initDomData = function () {
            var auctionIds = _this.auctionDom().auctionDomIds();
            for (var auctionIdIndex in auctionIds) {
                var auctionId = auctionIds[auctionIdIndex];
                var auctionObj = client.auction.createNG({'id': auctionId, 'status': 1});
                var auctionAttributes = auctionObj.getAttributes();
                var auctionInitData = {}
                var auctionDomSelect = _this.auctionDom().auction(auctionId);
                for (var attribute in auctionAttributes) {
                    auctionInitData[attribute] = auctionDomSelect.getAttribute(attribute);
                }
                auctionObj.setAttributes(auctionInitData);
                _this.setAuctionDispatcher(auctionObj);
            }
            return _this;
        }

        this._construct();
    }

    var auctionBroadcasts = function () {
        "use strict";
        this.broadcastsList = {}

        this.addDispatcherList = function (sendName, dispatcher) {
            this.broadcastsList[sendName] = dispatcher;
        }

        this.send = function (attribute, value, auctionDataObj) {
            var broadcastsList = this.broadcastsList;
            for (var broadcast in broadcastsList) {
                broadcastsList[broadcast](attribute, value, auctionDataObj);
            }
        }
    }

    var auctionData = function (auctionId) {
        "use strict";
        _baseEvent.apply(this);
        var _this = this;
        this.id = 0;
        this.attributes = {
            'title': '',
            'winner_price': '',
            'winner_name': '',
            'winner_avatar': '',
            'last_time': 0,
            'last_date': 0,
            'end_time': 0,
            'status': 0,
            'images': null,
            'url': null
        }
        this._broadcasts = false;

        this._construct = function (auctionId) {
            if (typeof auctionId != 'undefined') {
                this.id = auctionId;
            }

            this.setBaseEvent('setAttributeAfter',function(attribute,value){
            	if(attribute == 'status'){
            		switch(value){
            			case '2':
            				var shelfAll = client.shelf.all();
            				for(var shelfId in shelfAll){
            					shelfAll[shelfId].putOff(_this.id);
            				}
            				client.auction.del(_this.id);
            			break;
                	}
            	}
            	
            });
            
        }

        this.init = function (init) {

        }

        this.broadcasts = function () {
            if (this._broadcasts === false) {
                this._broadcasts = new auctionBroadcasts();
            }
            return this._broadcasts;
        }

        this.setAttribute = function (attribute, value) {
            if (this.attributes.hasOwnProperty(attribute)) {
                if (_this.hasBaseEvent(attribute + '.setFormat')) {
                    _this.attributes[attribute] = _this.runBaseEvent(attribute + '.setFormat', [attribute, value]);
                } else {
                    _this.attributes[attribute] = value;
                }
                this.broadcasts().send(attribute, _this.attributes[attribute], _this);
                if (_this.hasBaseEvent('setAttributeAfter')) {
                	_this.runBaseEvent('setAttributeAfter', [attribute, _this.attributes[attribute], _this]);
                }
                
            }
        }

        this.getAttribute = function (attribute) {
            return this.attributes[attribute];
        }

        this.setAttributes = function (attributes) {
            for (var attribute in attributes) {
                if (this.attributes.hasOwnProperty(attribute)) {
                    this.setAttribute(attribute, attributes[attribute]);
                }

            }
        }

        this.getAttributes = function (attribute) {
            return this.attributes;
        }

        this.runLastTime = function (interval) {
            this.updateLastTime(this.getAttribute('last_time') - interval);
        }

        this.severLastTime = function (serverTime) {

            this.updateLastTime(this.getAttribute('end_time') - serverTime);
        }

        this.updateLastTime = function (lastTime) {
            this.setAttribute('last_time', lastTime);
            if (this.getAttribute('last_time') <= 20) {
                var last_Date = '<span class="critical">' + this._buildLastDate(this.getAttribute('last_time')) + '</span>';
            } else {
                var last_Date = this._buildLastDate(this.getAttribute('last_time'));
            }
            this.setAttribute('last_date', last_Date);
        }

        this._buildLastDate = function (lastTime) {
            var timeString = {
                'h': 0,
                'i': 0,
                's': 0
            };

            if (lastTime > 0) {
                timeString['d'] = Math.floor((lastTime / 3600) / 24);       //d
                timeString['h'] = Math.floor((lastTime / 3600) % 24);      //h
                timeString['m'] = Math.floor((lastTime / 60) % 60);      //m
                timeString['s'] = Math.floor(lastTime % 60);             //s
                if (timeString['h'] < 10) {
                    timeString['h'] = '0' + timeString['h'];
                }
                if (timeString['m'] < 10) {
                    timeString['m'] = '0' + timeString['m'];
                }
                if (timeString['s'] < 10) {
                    timeString['s'] = '0' + timeString['s'];
                }

                if (timeString['d'] >= 1) {
                    timeString['d'] += " D ";
                } else {
                    timeString['d'] = '';
                }
                return timeString['d'] + timeString['h'] + ":" + timeString['m'] + ":" + timeString['s'];
            } else {
                return '00:00:00';
            }
        }

        this._construct(auctionId);
    }

    client.component('detail', function () {
        "use strict";
        _baseHash.apply(this);

        this.create = function (name, init) {
            var detailObj = new detail();
            detailObj.name = name;
            this.set(name, detailObj);
            return detail;
        }

        this.set = function (name, detail) {
            return this.setHash(name, detail);
        }

        this.get = function (name) {
            return this.getHash(name);
        }

        this.has = function (name) {
            return this.hasHash(name);
        }

        this.all = function () {
            return this.getHashAll();
        }

        this.clear = function () {
            return this.clearHash();
        }
    });

    client.component('shelf', function () {
        "use strict";
        _baseHash.apply(this);
        var _this = this;
        this._construct = function () {
            client.engine.createTimerNG('engineShelfInfo',10, 'ajax').setRequest({
                'type': 'post',
                'dataType': 'json',
                'success': function (response) {
                	
                	var shelfAll = client.shelf.all();
                	for(var shelfId in shelfAll){
                		var shelfDomIds = shelfAll[shelfId].auctionDom().auctionDomIds();
                		for(var shelfAuctiondomId in shelfDomIds){
                			var auctionId = shelfDomIds[shelfAuctiondomId];
                			var status = shelfAll[shelfId].auctionDom().auction(auctionId).getAttribute('status');
                			if(typeof status != 'undefined' && status == 2){
                				shelfAll[shelfId].auctionDom().auction(auctionId).getCurrentAuction().remove();
                			}
                		}
                	}
                	
                	
                    for (var shelfName in response) {
                        for (var auctioDataId in response[shelfName]['data']) {
                            var auctionCreateData = response[shelfName]['data'][auctioDataId];
                            auctionCreateData.id = auctionCreateData.auction_id;
                            var auctionObj = client.auction.createNG(auctionCreateData);
                            var buildData = auctionObj.getAttributes();
                            buildData.id = auctionCreateData.id;
                            buildData.last_date = '-- : -- : --';
                            var auctionHtml = _this.get(response[shelfName].name).auctionDom().buildHtmlTemplate(buildData);
                            var hasHtml = _this.get(response[shelfName].name).auctionDom().auction(auctionCreateData.id).getCurrentAuction();
                            if (hasHtml.length <= 0) {
                                _this.get(response[shelfName].name).auctionDom().root().find('.empty').remove();
                                _this.get(response[shelfName].name).auctionDom().root().append(auctionHtml);
                            }
                            _this.get(response[shelfName].name).putOn(auctionObj);
                        }
                    }
                },
                'complete': function () {
                }
            }).setBaseEvent('ajax.request.before', function (engine) {
                var url = client.engine.config('engineShelfInfo.url');
                engine.setRequest({'url': url});

                var checkRequest = false;
                var shelfPool = _this.all();
                for (var shelfName in shelfPool) {
                    checkRequest = shelfPool[shelfName].check();
                }
                return !(checkRequest === false);
            }).setRequestData(function () {
                var sendData = {}
                var shelfPool = _this.all();
                for (var shelfName in shelfPool) {
                    sendData[shelfName] = shelfPool[shelfName].check();
                }
                return sendData;
            });


        }

        this.create = function (name, init) {
            var createShelf = new shelf();
            createShelf.name = name;
            createShelf.init(init);
            this.set(name, createShelf)
            return createShelf;
        }

        this.createNG = function (name, init) {
            var shelfObj = false;
            if (this.has(name)) {
                shelfObj = this.get(name);
            } else {
                shelfObj = _this.create(name, init);
            }
            return shelfObj;
        }

        this.set = function (name, shelf) {
            return this.setHash(name, shelf);
        }

        this.get = function (name) {
            return this.getHash(name);
        }

        this.has = function (name) {
            return this.hasHash(name);
        }

        this.all = function () {
            return this.getHashAll();
        }

        this.clear = function () {
            return this.clearHash();
        }

        this._construct();
    });

    jQuery.fn.auctionDetail = function (init) {
        var _this = $(this);
        var _detailName = _this.selector;
        if (typeof init != 'undefined') {
            client.detail.create(_detailName, init);
        }

        return client.detail.get(_detailName);
    }

    jQuery.fn.shelf = function (init) {
        var _this = $(this);
        var _shelfName = _this.selector;
        if (typeof init != 'undefined') {
            init['rootSelector'] = _shelfName;
            client.shelf.createNG(_shelfName, init);
        }
        return client.shelf.get(_shelfName);
    }

    client.component('auction', function () {
        "use strict";
        _baseComponent.apply(this);
        var _this = this;
        this.data_ver = null;
        this._construct = function () {
            client.engine.createTimerNG('engineAuctionInfo', 1, 'ajax').setRequest({
                'data': {
                    'ids': function () {
                        var auctions = _this.all();
                        var auctionIds = [];
                        for (var auction in auctions) {
                            auctionIds.push(auction);
                        }
                        auctionIds = auctionIds.join('|');
                        return auctionIds;
                    },
                    'ver': function () {
                        return _this.data_ver;
                    },
                },
                'type': 'post',
                'dataType': 'json',
                'success': function (response) {
                    if (response.hasOwnProperty('rData')) {
                        for (var rDataId in response.rData) {
                            if (_this.has(response.rData[rDataId].id)) {
                                _this.get(response.rData[rDataId].id).setAttributes(response.rData[rDataId]);
                            }

                        }
                    }
                    if (response.hasOwnProperty('ver')) {
                        _this.data_ver = response['ver'];
                    }

                    if (response.hasOwnProperty('t')) {
                        var auctions = _this.all();
                        for (var auction in auctions) {
                            auctions[auction].severLastTime(response.t);
                        }
                    }
                },
                'complete': function () {
                }
            }).setBaseEvent('ajax.request.before', function (engine) {
                var url = client.engine.config('engineAuctionInfo.url');
                engine.setRequest({'url': url});
                return true;
            })

        }

        this.create = function (auctionInitData) {
            if (auctionInitData.hasOwnProperty('id')) {
                var createAuction = new auctionData(auctionInitData.id);
                createAuction.setAttributes(auctionInitData);
                this.set(createAuction.id, createAuction);
            }
            return createAuction;
        }

        this.createNG = function (auctionData) {
            var auctionObj = false;
            if (this.has(auctionData.id)) {
                auctionObj = this.get(auctionData.id);
            } else {
                auctionObj = _this.create(auctionData);
            }
            return auctionObj;
        }

        this.set = function (id, auction) {
            return this.setHash(id, auction);
        }

        this.get = function (id) {
            return this.getHash(id);
        }

        this.has = function (id) {
            return this.hasHash(id);
        }

        this.clear = function () {
            return this.clearHash();
        }

        this.del = function (id) {
            return this.delHash(id);
        }

        this.all = function () {
            return this.getHashAll();
        }

        this._construct();

    });

})(client, jQuery);
// bid auction namespace
(function(client){
	var _bidLock = {}
	
	var bidAuction = function(){
        _baseComponent.apply(this);
		var _this = this;
        this.auctionId = 0;
        this.ajaxRequest = {
    		'url':'',
    		'type':'post',
    		'dataType':'json',
    		'complete':function(){
    			
    		}
    	}
        
        this.init = function(bidConfig){
    		for(var bidConfigKey in bidConfig){
    			this.ajaxRequest[bidConfigKey] = bidConfig[bidConfigKey];
    		}
    		if(bidConfig.hasOwnProperty('event')){
    			for(var eventKey in bidConfig['event']){
    				_this.setBaseEvent(eventKey,bidConfig['event'][eventKey]);
    			}
    		}
    		this.ajaxRequest['complete'] = function(response){
    			_this.runBaseEvent('bidAjax.complete',[response]);
    			_this.btnUnLock();
    		}
    		this.ajaxRequest['success'] = function(response){
    			_this.runBaseEvent('bidAjax.success',[response]);
    		}
    		this.ajaxRequest['error'] = function(response){
    			_this.runBaseEvent('bidAjax.error',[response]);
    		}
    		return this;
    	}
        
		this.bid = function(){
			_this.ajaxRequest['data'] = {
				'auctionId':_this.auctionId
			}
			if(!_this.btnLock()){
				$.ajax(_this.ajaxRequest);
			}
			
			
		}
		
		
		
		this.btnLock = function(){
			
			if(!_bidLock.hasOwnProperty(_this.auctionId)){
				_bidLock[_this.auctionId] = setTimeout(function(){
					_this.btnUnLock();
				},5000);
				return false;
			}else{
				return true;
			}
		}
		
		this.btnUnLock = function(){
			return delete _bidLock[_this.auctionId];
		}
	}
	
	client.component('bidAuction', function () {		
		var bidAuctionObjPool = {}
		this.bidAuctionInit = {}
		this.init = function(init){
			this.bidAuctionInit = init;
			return this;
		}
		this.get = function(auctionId){
			if(!bidAuctionObjPool.hasOwnProperty(auctionId)){
				var bidAuctionObj = new bidAuction();
				bidAuctionObj.auctionId = auctionId;
				bidAuctionObjPool[auctionId] = bidAuctionObj;
			}
			return bidAuctionObjPool[auctionId].init(this.bidAuctionInit);
		}
	});
	
})(client);